﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Xml;
using System.Xml.Serialization;
using Fcdbas.Core;
using Fcdbas.Core.DataAccess;
using Fcdbas.Platform.Enums;
using Fcdbas.Core.Reflection;
using Fcdbas.Core.Util;
using Fcdbas.Platform.Design;
namespace Fcdbas.Platform.Properties
{
    public enum FieldType
    {
        varchar,
        datetime, 
        number,
        blob,
        Null

    }
    [Serializable]
    // [TypeConverter(typeof(DataItemConverter))]
    public class DataItem : ICloneable, IComponentPropertyChanged
    {
        public DataItem(string columnName, Type dType, int length, string lparent, bool simple, bool useEditor, bool qable, bool onlywhere, string validate, string reference, string emetho, string caption, string format, bool req, bool insertable, DataItemFlags flags, DataItemUpdate update)
        {
            this.ColumnName = columnName;
            this.DataType = dType;
            this.Length = length;
            this.LogicalParent = lparent;
            this.Simple = simple;
            this.UseEditor = useEditor;
            this.Queryable = qable;
            this.OnlyInWhereClause = onlywhere;
            this.Validate = validate;
            this.Reference = reference;
            this.EnumMethod = emetho;
            this.Caption = caption;
            this.FormatOption = format;
            this.Required = req;
            this.Insertable = insertable;
            this.Flags = flags;
            this.UpdateOption = update;
        }
        private string _ColumnName;
        [Description("列名"), DisplayName("列名"), XmlAttribute]
        public string ColumnName
        {
            get
            {
                return this._ColumnName;
            }
            set
            {
                this._ColumnName = value;
            }
        }
        [DisplayName("标题")]
        [Description("标题")]
        public string Caption
        {
            get;
            set;
        }
        [DisplayName("键标志")]
        [Description("键标志")]
        public DataItemFlags Flags
        {
            get;
            set;
        }
        [DisplayName("更新选项")]
        [Description("更新选项")]
        public DataItemUpdate UpdateOption
        {
            get;
            set;
        }
        [DisplayName("必须")]
        [Description("必须")]
        public bool Required
        {
            get;
            set;
        }
        [DisplayName("格式化")]
        [Description("格式化")]
        [TypeConverter(typeof(DropDownListConverter))]
        public string FormatOption
        {
            get;
            set;
        }

        [DisplayName("可插入")]
        [Description("可插入")]
        public bool Insertable
        {
            get;
            set;
        }

        [DisplayName("是否可见")]
        [Description("是否可见")]
        public bool OnlyInWhereClause
        {
            get;
            set;
        }
        [DisplayName("下拉")]
        [Description("下拉")]
        public bool PropertyChanged
        {
            get;
            set;
        }
        [DisplayName("下拉可见")]
        [Description("下拉可见")]
        public bool Queryable
        {
            get;
            set;
        }

        [DisplayName("父单元")]
        [Description("父单元")]
        public string LogicalParent
        {
            get;
            set;
        }
        [DisplayName("单选框")]
        [Description("单选框")]
        public bool Simple
        {
            get;
            set;
        }
        [DisplayName("多行文本")]
        [Description("多行文本")]
        public bool UseEditor
        {
            get;
            set;
        }
        [DisplayName("图片")]
        [Description("图片")]
        public bool IsImage
        {
            get;
            set;
        }
        [DisplayName("枚举方法")]
        [Description("枚举方法")]
        public string EnumMethod
        {
            get;
            set;
        }

        [DisplayName("增加到界面")]
        [Description("增加到界面")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public bool AddToForm
        {
            get;
            set;
        }
        [DisplayName("富文本")]
        [Description("富文本")]
        public bool IsRichEdit
        {
            get;
            set;
        }
        [DisplayName("二进制")]
        [Description("二进制")]
        public bool IsBlobEdit
        {
            get;
            set;
        }
        [DisplayName("校验函数")]
        [Description("校验函数")]
        public string Validate
        {
            get;
            set;
        }
        [DisplayName("引用字段")]
        [Description("引用字段")]
        public string Reference
        {
            get;
            set;
        }
        [DisplayName("引用函数")]
        [Description("引用函数")]
        public string RefFunction
        {
            get;
            set;
        }
        [DisplayName("函数定义")]
        [Description("函数定义")]
        public string FunctionDefine
        {
            get;
            set;
        }
        [DisplayName("自定义格式")]
        [Description("自定义格式")]
        public string CustomerFormat
        {
            get;
            set;
        }
        public DataItem()
            : base()
        {
            Queryable = true;
            Flags = DataItemFlags.Attribute;
            Required = false;
            Insertable = true;
            AddToForm = true;
            UpdateOption = DataItemUpdate.Allow;
        }
        #region 属性
        [Browsable(false)]
        [XmlAttribute]
        public int Alignment
        {
            get;
            set;
        }
        private String _Name;
        /// <summary>名称</summary>
        [Browsable(false)]
        [XmlAttribute]
        [DisplayName("名称")]
        [Description("名称")]
        public String Name
        {
            get
            {
                string name;
                if (!string.IsNullOrEmpty(this._Name))
                {
                    name = this._Name;
                }
                else
                {
                    this._Name = this.ColumnName;

                    name = this._Name;
                }
                return name;
            }
            set
            {
                this._Name = value;
            }
        }
        private bool _isAssist = false;
        [DisplayName("辅助字段")]
        [Description("辅助字段")]
        public bool IsAssist
        {
            get
            {
                return _isAssist;
            }
            set
            {
                _isAssist = value;
            }
        }

        private Type _DataType;
        /// <summary>数据类型</summary>
        
        [XmlAttribute]
        [Browsable(false)]
        [DisplayName("数据类型")]
        [Description("数据类型")]
        public Type DataType { get { return _DataType; } set { _DataType = value; } }

        [DisplayName("数据类型")]
        [Description("数据类型")]
        public FieldType FieldType
        {
            get
            {
                if (_DataType == typeof(string))
                    return Properties.FieldType.varchar;
                else if (_DataType == typeof(byte[]))
                    return Properties.FieldType.blob;
                else if (_DataType == typeof(DateTime))
                    return Properties.FieldType.datetime;
                else if (_DataType == typeof(int))
                    return Properties.FieldType.number;
                else
                    return Properties.FieldType.Null;

            }
            set
            {
                if (value == Properties.FieldType.varchar)
                    _DataType = typeof(string);
                else if (value == Properties.FieldType.datetime)
                    _DataType = typeof(DateTime);
                else if (value == Properties.FieldType.blob)
                    _DataType = typeof(DateTime);
                else
                    _DataType = typeof(int);
            }
        }
        private Boolean _PrimaryKey;
        /// <summary>主键</summary>
        [XmlAttribute]
        [Browsable(false)]
        [DisplayName("主键")]
        [Description("主键")]
        public Boolean PrimaryKey { get { return _PrimaryKey; } set { _PrimaryKey = value; } }

        private Int32 _Length;
        /// <summary>长度</summary>
        [XmlAttribute]
        [DisplayName("长度")]
        [Description("长度")]
        public Int32 Length { get { return _Length; } set { _Length = value; } }


        private Int32 _Precision;
        /// <summary>精度</summary>
        [XmlAttribute]
        [Browsable(false)]
        [DisplayName("精度")]
        [Description("精度")]
        public Int32 Precision { get { return _Precision; } set { _Precision = value; } }

        private Int32 _Scale;
        /// <summary>位数</summary>
        [XmlAttribute]
        [Browsable(false)]
        [DisplayName("位数")]
        [Description("位数")]
        public Int32 Scale { get { return _Scale; } set { _Scale = value; } }

        private Boolean _Nullable;
        /// <summary>允许空</summary>
        [XmlAttribute]
        [Browsable(false)]
        [DisplayName("允许空")]
        [Description("允许空")]
        public Boolean Nullable { get { return _Nullable; } set { _Nullable = value; } }

        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string OldName
        {
            get;
            set;
        }

        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string OldColumnName
        {
            get;
            set;
        }

        private String _Default;
        /// <summary>默认值</summary>
        [XmlAttribute]
        [DisplayName("默认值")]
        [Description("默认值")]
        public String Default { get { return _Default; } set { _Default = value; } }

        private String _Description;
        /// <summary>说明</summary>
        [XmlAttribute]
        [DisplayName("说明")]
        [Description("说明")]
        public String Description
        {
            get { return _Description; }
            set
            {
                if (!String.IsNullOrEmpty(value))
                    value = value.Replace("\r\n", "。")
                        .Replace("\r", " ")
                        .Replace("\n", " ");
                _Description = value;
            }
        }
        #endregion
        public DataItem(string name)
            : base()
        {
            Name = name;
            AddToForm = true;
            Queryable = true;
        }

        public DataItem(object entityMember, string name, bool primaryKey)
            : base()
        {
            Name = name;
            Queryable = true;
            this.PrimaryKey = primaryKey;
            Flags = primaryKey ?
                    Fcdbas.Platform.Enums.DataItemFlags.Key :
                    Fcdbas.Platform.Enums.DataItemFlags.Attribute;
            Required = false;
            Insertable = true;
            AddToForm = true;
            UpdateOption = Fcdbas.Platform.Enums.DataItemUpdate.Allow;
            Type t = entityMember.GetType();
            BindColumnAttribute colAtrribute = t.GetCustomAttribute<BindColumnAttribute>(true);
            DisplayNameAttribute disAttib = t.GetCustomAttribute<DisplayNameAttribute>(true);


            ColumnName = colAtrribute.Name;
            Name = colAtrribute.Name;
            DataType = entityMember.GetType();
            Length = colAtrribute.MaxLength;
            //MaxLength = sourceColumn.MaxLength;
        }

        #region ICloneable 成员

        public object Clone()
        {
            return Fcdbas.Core.Util.ObjectHelper.CloneObject(this);
        }

        #endregion
    }
}
